package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.ChartCategoryDTO;
import com.xbongbong.paas.pojo.ChartSaveRuleDTO;
import com.xbongbong.paas.pojo.dto.ChartSetDetailDTO;
import com.xbongbong.paas.pojo.dto.ChartSetEnableDTO;
import com.xbongbong.paas.pojo.dto.ChartSetSaveDTO;
import com.xbongbong.paas.pojo.vo.ChartSaveRuleVO;
import com.xbongbong.paas.pojo.vo.ChartSetVO;
import com.xbongbong.paas.service.PkSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.domain.entity.ChartCategoryEntity;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author youli.chen
 * @ProjectName xbb-pro
 * @Description: PK榜设置控制类
 * @date 2019/1/22 20:01
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/pk/set")
public class PkSetController {

    @Resource
    private PkSetService pkSetService;

    /**
     * 【web】获取PK榜设置
     *
     * @param chartSetDetailDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getConfig(@RequestBody @Valid ChartSetDetailDTO chartSetDetailDTO, BindingResult br) {
        XbbResponse<ChartSetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.pkSetService.getChartSet(chartSetDetailDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】启用/关闭pk榜指标
     *
     * @param chartSetEnableDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid ChartSetEnableDTO chartSetEnableDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.pkSetService.chartSetEnable(chartSetEnableDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】保存指标名称
     *
     * @param chartSetSaveDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveName(@RequestBody @Valid ChartSetSaveDTO chartSetSaveDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.pkSetService.saveName(chartSetSaveDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】保存PK榜设置
     *
     * @param chartSaveRuleDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveRule", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveRule(@RequestBody @Valid ChartSaveRuleDTO chartSaveRuleDTO, BindingResult br) {
        XbbResponse<ChartSaveRuleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.pkSetService.saveRule(chartSaveRuleDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取所有分类,pk榜用
     * @param chartCategoryDTO
     * @param br
     * @return java.lang.String
     * @throws XbbException
     * @author xinpeng.jiang
     * @date 2021/5/13 19:39
     * @version 1.0
     */
    @RequestMapping(value = "/getCategoryList4Pk", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCategoryList4Pk(@RequestBody @Valid ChartCategoryDTO chartCategoryDTO, BindingResult br) {
        XbbResponse<List<ChartCategoryEntity>> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                List<ChartCategoryEntity> categoryList4Pk = pkSetService.getAllCategoryList(chartCategoryDTO);
                response = new XbbResponse<>(categoryList4Pk);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
